/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : NumberRounder.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Nov 14, 2008       Raghu Sarraff			       Created
 */
package net.lwf.number.util;

import java.util.HashMap;
import java.util.Map;

import net.lwf.common.util.PrintUtil;



/**
 * Type the class description here
 *  
 */
public class NumberRounder {
	
	public static Map<String, String> getRoundedData(int number) {
		Map<String, String> mapdata = new HashMap<String, String>();
		String sNumbar = String.valueOf(number);
		int nLen = sNumbar.length();
		
		int factor = 0;
		String sfactor = "1";
		for(int i=0; i<nLen-1; i++) {
			sfactor += "0";
		}
		factor = Integer.parseInt(sfactor);
		while(factor >= 10) {
			double fraction = (double) number/factor;
			long rounded = Math.round(fraction);
			mapdata.put(String.valueOf(factor), String.valueOf(rounded*factor));
			factor = factor/10;
		}
		return mapdata;
	}
	
	public static void main(String args[]) {
		int number = RandomNumberGenerator.generate(1, 99999);
		System.out.println("=====> Generated number: " + number);
		getRoundedData(number);
		PrintUtil.printMap(numberPosition(number));
	}
	
	public static Map<String, String> numberPosition(int number) {
		String sNumber = String.valueOf(number);
		int nLen = sNumber.length();
		Map<String, String> mapData = new HashMap<String, String>(nLen);
		
		for(int i=0; i<nLen; i++) {
			String value = null;
			if(i==0) {
				value = sNumber.substring(0, 1);
			}
			else {
				value = sNumber.substring(i,i+1);
			}
			mapData.put(String.valueOf(nLen-i), value);
		}		
		/*
		
		int nCounter = 1;
		while(nLen > 0) {
			String s = String.valueOf(sNumber.charAt(nLen-1));
			mapData.put(String.valueOf(nCounter), s);
			nLen = nLen - 1;
			nCounter++;
		}*/
		PrintUtil.printMap(mapData);
		return mapData;
	}
	

} //NumberRounder

